package com.gateway.advice;

import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
public class ErrorWeb implements ErrorWebExceptionHandler {
    @Override
    public Mono<Void> handle(ServerWebExchange exchange, Throwable throwable) {
        ServerHttpResponse response = exchange.getResponse();
//        BaseResponse baseResponse = new BaseResponse();
//        if (ex instanceof ResponseStatusException) {
//            baseResponse.setStatus(((ResponseStatusException) ex).getStatus().value());
//            baseResponse.setMessage(((ResponseStatusException) ex).getReason());
//        } else {
//            baseResponse.setStatus(500);
//            baseResponse.setMessage(ex.getMessage());
//        }
//        DataBuffer dataBuffer = response.bufferFactory()
//                .allocateBuffer().write(JSONUtils.toJSONString(baseResponse).getBytes());
//        response.setStatusCode(HttpStatus.OK);
//        //基于流形式
//        response.getHeaders().setContentType(MediaType.APPLICATION_NDJSON);
//        return response.writeAndFlushWith(Mono.just(ByteBufMono.just(dataBuffer)));

        return null;
    }
}
